package org.six11.ss.client;

import org.six11.ss.client.olive.OliveState;
import org.six11.ss.shared.GameStatusConstants;
import org.six11.ss.shared.Message;
import org.six11.util.adt.MultiState;

/**
 * 
 * 
 * @author Gabe Johnson <johnsogg@cmu.edu>
 */
public class GameStatusMessageHandler extends AWTMessageHandler {

  /**
   * @param state
   */
  public GameStatusMessageHandler(MultiState state) {
    super(state);
  }

  public void handleAWT(Message msg) {
    String status = msg.getParam("status");
    String when = "an unknown amount of time. MYSTERY!";
    long whenMS = Long.MAX_VALUE;
    if (msg.getParam("when") != null) {
      try {
        String ms = msg.getParam("when");
        whenMS = Long.parseLong(ms);
        when = "about " + (whenMS / 1000L) + " seconds";
      } catch (NumberFormatException ex) {
        getConsole().addStatus(
            "The server responded with a bogus round length: " + msg.getParam("when"));
        ex.printStackTrace();
      }
    }
    long whenElapsed = System.currentTimeMillis() + whenMS;
    state.setValue(ClientStateNames.K_ROUND_LENGTH, whenMS);
    bug("Setting round elapsed variable to: " + whenElapsed);
    state.setValue(ClientStateNames.K_ROUND_ELAPSED, whenElapsed);
    state.setValue(ClientStateNames.K_GAME_STATUS, status);

    // during particular game phases, clear state data
    if (status.equals(GameStatusConstants.STATUS_WARN) ||
        status.equals(GameStatusConstants.STATUS_LOBBY) ||
        status.equals(GameStatusConstants.STATUS_GAME_STATS)) {
      // clear the sketch
      OliveState oliveState = (OliveState) state.getValue(ClientStateNames.K_OLIVE_STATE);
      oliveState.clearSurface();
      
      // remove old labels
      state.setValue(ClientStateNames.K_LABELS, null);
      
      // forget the last clue
      state.setValue(ClientStateNames.K_CLUE, null);
    }

  }
}
